﻿using ACC.Extensions.ClientBusiness;
using ACC.Extensions.PCClient.Helper;
using ACC.Extensions.PCClient.View;
using ACC.Extensions.Utilities;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ACC.Extensions.PCClient.UserControls
{
    /// <summary>
    /// Interaction logic for NavBarControl.xaml
    /// </summary>
    public partial class NavBarControl : UserControl
    {
        private Thread _alarmThread;
        private bool _bAlarm;
        private bool _bIsRuning;
        private bool _bIsThreadRunning = false;
        public NavBarControl()
        {
            InitializeComponent();
        }

        public void StartAlarmThread()
        {
            if (_alarmThread==null)
            {
                _bIsThreadRunning = true;
                _alarmThread = new Thread(new ThreadStart(HighLight));
                _bIsRuning = true;
                _alarmThread.IsBackground = true;
                _alarmThread.Start();
            }

            _bIsRuning = true;
        }

        public void StopAlarm()
        {
            _bIsRuning = false;
        }

        public void StopAlarmThread()
        {
            _bIsThreadRunning = false;

            if (_alarmThread != null)
            {
                _alarmThread.Join(1000);
                if (_alarmThread.IsAlive)
                    _alarmThread.Abort();
            }
        }
        private void HighLight()
        {
            while (_bIsThreadRunning)
            {
                if (_bIsRuning == true)
                {
                    if (_bAlarm == false)
                    {
                        Dispatcher.BeginInvoke(new Action(() => { 
                           this.txtBarcode.Background = new SolidColorBrush(Colors.Yellow);
                           //this.txtPartNO.Background = new SolidColorBrush(Colors.Yellow); 
                        }));
                        _bAlarm = true;
                    }
                    else
                    {
                        Dispatcher.BeginInvoke(new Action(() => {
                            this.txtBarcode.Background = new SolidColorBrush(Colors.Red);
                           // this.txtPartNO.Background = new SolidColorBrush(Colors.Red);
                        }));
                        _bAlarm = false;
                    }
                }
                else
                {
                    Dispatcher.BeginInvoke(new Action(() => { txtBarcode.Background = new SolidColorBrush(Colors.White); }));
                }
                Thread.Sleep(500);
            }
        }

        private void txtBarcode_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void miHardnessRes_Click(object sender, RoutedEventArgs e)
        {
            //HardnessResultWnd wnd = new HardnessResultWnd();
            //var dialogResult = wnd.ShowDialog();
        }

        private void miProducePlan_Click(object sender, RoutedEventArgs e)
        {
            
        }

    }
}
